﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using ESF.General;
using Microsoft.Ajax.Utilities;

#endregion


namespace ESF.Web.Handlers
{
	public class CSSCompression : IHttpHandler
	{
		#region Metodos de IHttpHandler

		public bool IsReusable
		{
			get { return true; }
		}


		public void ProcessRequest(HttpContext pContext)
		{
			string fileName;
			string contents;

			Minifier minifier;

			FileInfo fileInfo;

			string eTag;

						
			fileName = pContext.Request.PhysicalPath;
						
			if (Path.GetFileNameWithoutExtension (fileName).ToUpper ().StartsWith ("jquery"))
			{
				return;
			}

			
			ModuleHandlerHelper.AddESFDescriptionHeader(pContext, "CSSCompressed"); 
			

			if (ModuleHandlerHelper.GeneratedResponse_SetETagCacheIs304File(pContext, fileName, null, 60, out fileInfo, out eTag))
			{
				return;
			}


			if (HttpContext.Current.Application ["CSSCompressed_" + eTag] == null)
			{
				HttpContext.Current.Trace.Write("Nuevo: CSSCompressed_" + eTag); 
				
				minifier = new Minifier();

				contents = minifier.MinifyStyleSheet(File.ReadAllText(fileName)); 
				
				
				HttpContext.Current.Application.Add ("CSSCompressed_" + eTag, contents);

				HttpContext.Current.Trace.Write("Guardado: CSSCompressed_" + eTag); 
			}
			else
			{
				HttpContext.Current.Trace.Write("Leido: CSSCompressed_" + eTag); 

				contents = HttpContext.Current.Application ["CSSCompressed_" + eTag] as string;
			}


			ModuleHandlerHelper.AddZipFilter(pContext);

			pContext.Response.Write (contents);
		}

		#endregion
	}
}
